//
//  CollectViewController.m
//  MMLOOK
//
//  Created by dllo on 15/9/3.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBase.h"
#import "CollectTableViewCell.h"
#import "WedModel.h"
#import "WedDetailViewController.h"


@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic, retain)NSMutableArray *arr;

@property(nonatomic, retain)UIView *whiteView;
@property(nonatomic, retain)UIButton *allSelect;
@property(nonatomic, retain)UIButton *cancelButton;

@property(nonatomic, assign)BOOL flag; //记录点击状态
@property(nonatomic, assign)BOOL allSelectFlage; //是否全选的状态

@property(nonatomic, retain)NSMutableArray *deletArr;



@end

@implementation CollectViewController

- (void)dealloc
{
   
    [_myTable release];
    [_arr release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.arr = [NSMutableArray array];
        self.deletArr = [NSMutableArray array];
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.view.backgroundColor = [UIColor cyanColor];

    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:253 / 255.0 green:180/255.0 blue:83/255.0 alpha:1];
    
    self.navigationItem.title = @"我的收藏";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.tabBarController.tabBar.translucent = YES;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"edit.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
    
    //添加到数组中
    [self.arr addObjectsFromArray:[[[DataBase selectAll] reverseObjectEnumerator] allObjects]];
    
    
    //创建tableView
    [self creatCollect];
    
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.arr removeAllObjects];
    
    //添加到数组中
    [self.arr addObjectsFromArray:[[[DataBase selectAll] reverseObjectEnumerator] allObjects]];
    
    [self.myTable reloadData];
    
    
}


-(void)cancelAction
{
    
    if (self.flag == NO) {
        
#warning 编辑
        
    self.flag = YES;
        
    [self.myTable setEditing:YES animated:YES];
        
    self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"delete.png"];
        
     //取消
    [self creatWhileView];
    
        
    } else {
        
#warning 删除
        
        self.flag = NO;
        
        [self.myTable setEditing:NO animated:YES];
        
         self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"edit.png"];
    
        [self.whiteView removeFromSuperview];
        
        
        //有删除的东西
        if (self.deletArr.count != 0) {
            
            [self deleteSome];
        }
        
      

    }
    
}




-(void)creatWhileView
{
    
    self.whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    self.whiteView.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    [self.view addSubview:self.whiteView];
    [_whiteView release];
    
    
    self.allSelect = [UIButton buttonWithType:UIButtonTypeSystem];
    self.allSelect.frame = CGRectMake(0 , 10, self.view.frame.size.width / 2, 30);
    [self.allSelect setTitle:@"全选" forState:UIControlStateNormal];
    [self.allSelect addTarget:self action:@selector(allSelectButton) forControlEvents:UIControlEventTouchUpInside];
    [self.whiteView addSubview:self.allSelect];
    
    UIView *blackView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2, 0, 0.5, 50)];
    blackView.backgroundColor = [UIColor blackColor];
    [self.whiteView addSubview:blackView];
    [blackView release];
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.cancelButton.frame = CGRectMake(self.view.frame.size.width / 2, 10, self.view.frame.size.width / 2, 30);
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.whiteView addSubview:self.cancelButton];
    
    
}


#pragma 确定删除

-(void)deleteSome
{
    
    for (WedModel *model in self.deletArr) {
        
        [DataBase deleteWithModel:model];
        
    }
    
    //tableArr 全删除
    [self.arr removeAllObjects];
    
    //重新 添加到数组中
    [self.arr addObjectsFromArray:[[[DataBase selectAll] reverseObjectEnumerator] allObjects]];
    
    //刷新
    [self.myTable reloadData];
    
    
}




#pragma 全选按钮
-(void)allSelectButton
{
    
    //选择全选
    if (self.allSelectFlage == NO) {
        
        self.allSelectFlage = YES;
        
        [self.allSelect setTitle:@"取消全选" forState:UIControlStateNormal];
        
        [self.deletArr removeAllObjects];
        
        for (WedModel *model in self.arr) {
            
            [self.deletArr addObject:model];
            
        }
        
        //刷新
        [self.myTable reloadData];

    } else {
        
         self.allSelectFlage = NO;
        
        [self.allSelect setTitle:@"全选" forState:UIControlStateNormal];
        
        [self.deletArr removeAllObjects];

        //刷新
        [self.myTable reloadData];

        
    }
    
    
}



#pragma 取消按钮
-(void)cancelButtonAction
{
    
    [self.myTable setEditing:NO animated:YES];
    
    self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"edit.png"];
    
    [self.whiteView removeFromSuperview];
    
    //删除数组 清空 下次使用
    [self.deletArr removeAllObjects];
    
    self.flag =!self.flag;
    
}



-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //每一行都是什么编辑状态 (删除 / 添加) 批量 (添加和批量都不太常用)
    return UITableViewCellEditingStyleInsert
    | UITableViewCellEditingStyleDelete;
    
}



-(void)creatCollect
{
    
    self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 + 49) style:UITableViewStylePlain];
    
    self.myTable.delegate = self;
    
    self.myTable.dataSource = self;
    
    [self.view addSubview:self.myTable];
    
    [_myTable release];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 150;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *str = @"myTable";
    
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell == nil) {
        
        cell = [[[CollectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]autorelease];
        
    }
    
    WedModel *model = [self.arr objectAtIndex:indexPath.item];
    
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = model;
    
    
    
    if (self.flag == YES) {
        
        for (WedModel *wed in self.deletArr) {
            
            if ([wed isEqual:model]) {
                
                //全部选择状态
                [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                
            }
            
        }
        
    }
    
    
    
    if (self.allSelectFlage == YES) {
        
        //全部选择状态
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        
    }
    
    
    return cell;
    
}


//选择一个
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //编辑状态
    if (self.flag == YES) {
        
        WedModel *model = [self.arr objectAtIndex:indexPath.item];
        
        [self.deletArr addObject:model];
        
    } else {
        
        WedDetailViewController *wed = [[WedDetailViewController alloc]init];
        
        wed.model = [self.arr objectAtIndex:indexPath.item];
        
        [self.navigationController pushViewController:wed animated:YES];
        
        [wed release];

    }
    
    
    
}


//取消一个
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    //编辑状态
    if (self.flag == YES) {
        
        WedModel *model = [self.arr objectAtIndex:indexPath.item];
        
        [self.deletArr removeObject:model];
        
        //只要按了取消 就不在是全选状态
        self.allSelectFlage = NO;
        
        [self.allSelect setTitle:@"全选" forState:UIControlStateNormal];
        
    }

    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
